package com.itcast.tanhuasso.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.itcast.tanhuasso.config.HuanXinConfig;
import com.itcast.tanhuasso.service.HuanXinService;
import com.itcast.tanhuasso.service.HuanXinTokenService;
import com.itcast.tanhuasso.vo.HuanXinUser;
import org.apache.commons.codec.digest.DigestUtils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl;

import java.util.*;

/**
 * @PachageName: com.itcast.tanhuasso.service.impl
 * @ClassName:
 * @Description: TODO
 * @Author: Hushi
 * @DateTime: 2021/2/2114:36
 * @Version 1.0
 */
@Service
public class HuanXinServiceImpl implements HuanXinService {

    @Autowired
    private HuanXinConfig huanXinConfig;

    @Autowired
    private HuanXinTokenService huanXinTokenService;

    @Autowired
    private RestTemplate restTemplate;

    private static final ObjectMapper MAPPER = new ObjectMapper();
    @Override
    public Boolean register(Long userId) {
        String url = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/users";

        String token = this.huanXinTokenService.getToken();

        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        headers.add("Authorization","Bearer " + token);
        /*HttpHeads httpHead = new HttpHead();
        httpHead.setHeader("Content-Type","application/json");
        httpHead.setHeader("Authorization","Bearer " + token);*/

        List<HuanXinUser> body = new ArrayList<>();
        body.add(new HuanXinUser(String.valueOf(userId), DigestUtils.md5Hex(userId + "_itcast_tanhua")));
        try {
            HttpEntity<String> httpEntity = new HttpEntity(MAPPER.writeValueAsString(body),headers);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue() == 200;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public Boolean contactUsers(Long userId, Long friendId){
        String url = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/users/" + userId + "/contacts/users/" + friendId;
        try {
            String token = this.huanXinTokenService.getToken();
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("Authorization","Bearer " + token);
            HttpEntity<String> httpEntity = new HttpEntity(httpHeaders);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
            return responseEntity.getStatusCodeValue() == 200;
        } catch (RestClientException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Boolean sendMsg(String target, String msg, String type) {
        String url = huanXinConfig.getUrl() + huanXinConfig.getOrgName() + "/" + huanXinConfig.getAppName() + "/messages";
        try {
            String token = this.huanXinTokenService.getToken();
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization","Bearer " + token);

            Map<String,Object> parmmap = new HashMap<>();
            parmmap.put("target_type","users");
            parmmap.put("target", Arrays.asList(target));

            Map<String,Object> msgmap = new HashMap<>();
            msgmap.put("type",type);
            msgmap.put("msg",msg);
            parmmap.put("msg",msgmap);

            HttpEntity<String> entity = new HttpEntity<>(MAPPER.writeValueAsString(parmmap),headers);
            ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, entity, String.class);
            return responseEntity.getStatusCodeValue() == 200;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        return false;
    }
}
